package com.svmc.app.task.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.svmc.app.task.activity.R;
import com.svmc.app.task.model.TaskModel;


public class ListAdapter extends BaseAdapter{
	private static final int FINISH = 0;
	private static final int NOTFINISH =1;
	Context mcontext;
	ArrayList<TaskModel> listtask = new ArrayList<TaskModel>();
	private LayoutInflater mInflater;
	DbAdapter db;
	
    public ListAdapter(Context context, ArrayList<TaskModel> tasks) {
    	mcontext = context;
    	listtask = tasks;
    	mInflater = LayoutInflater.from(context);
    	db = new DbAdapter(context);
    }
    
	public int getCount() {
		
		return listtask.size();
	}

	public Object getItem(int position) {
		
		return listtask.get(position);
	}

	public long getItemId(int positon) {
		
		return positon;
	}

	public View getView(final int position, View convertview, ViewGroup parent) {
		ViewHolder holder;
		if (convertview == null) {
			mInflater = (LayoutInflater) mcontext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			 convertview = mInflater.inflate(R.layout.item_task, null);
			//convertview = mInflater.inflate(mLayouts[0], null);
			  holder = new ViewHolder();
			  holder.txtName =(TextView)convertview.findViewById(R.id.itemtask_txt_nametask);
			 // holder.txtName =(TextView)convertview.findViewById(mIds[0]);
			  holder.isSetReminder =(ImageView)convertview.findViewById(R.id.item_img_isreminder);
			  holder.isFinish =(CheckBox)convertview.findViewById(R.id.itemtask_cb_finish);
			  convertview.setTag(holder);
		 }else {
				holder =(ViewHolder)convertview.getTag();
		 }
		holder.txtName.setText(listtask.get(position).getTask());
		//holder.txtdate.setText(""+listtask.get(position).getTimefinish());
		if(listtask.get(position).getIsreminder()==1){
			holder.isSetReminder.setImageResource(R.drawable.icon_reminder);
		}else {
			holder.isSetReminder.setImageResource(R.drawable.icon_nothing);
		}
		
		holder.isFinish.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(mcontext, "check", 0).show();
					listtask.get(position).setStatus(NOTFINISH);
					// update status taskmodel when user tab checkbox.tab checkbox -> status = 0(task is finished);
						db.updateStatusTask(listtask.get(position));
				}else {
					Toast.makeText(mcontext, "uncheck", 0).show();
					listtask.get(position).setStatus(FINISH);
					// update status taskmodel when user tab checkbox.tab checkbox -> status = 1(task is not finished);
						db.updateStatusTask(listtask.get(position));
				}
				
			}
		});
		return convertview;
	}
	 static class ViewHolder {
	        TextView txtName;
	        TextView txtdate;
	        ImageView isSetReminder;
	        CheckBox isFinish;
	        
	    }
}
